#ifndef BOSSAPP_HPP
#define BOSSAPP_HPP

#include "general.h"
#include "cJSON.h"
#include "Function.hpp"

using namespace std;

class BossApp{

public:
	//打开数据库，没有则创建
	 void Sqlite3_open(){
		ret = sqlite3_open("sql.db", &db);
		if(ret != SQLITE_OK){
			cout << "sqlite3_open err" << sqlite3_errmsg(db) << endl;
			sqlite3_free(errmsg);
			
			return;
		}

		char *account_table = "create table if not exists Account_table(Account char primary key, Password char, Question char, Answer char)";
		ret = sqlite3_exec(db, account_table, NULL, NULL, &errmsg);
		if(ret != SQLITE_OK){
			cout << "sqlite3_exec create table err:" << sqlite3_errmsg(db) << endl;
			sqlite3_free(errmsg);
			
			return;
		}

		char *machine_table = "create table if not exists Machine_table(MachineId char primary key, Password char, Master char)";
		ret = sqlite3_exec(db, machine_table, NULL, NULL, &errmsg);
		if(ret != SQLITE_OK){
			cout << "sqlite3_exec create table err:" << sqlite3_errmsg(db) << endl;
			sqlite3_free(errmsg);

			return;
		}
			
		char *commodity_table = "create table if not exists Commodity_table(MachineId char, GoodsId char, Name char, Price double, Stock int, Image char, Type char)";
		ret = sqlite3_exec(db, commodity_table, NULL, NULL, &errmsg);
		if(ret != SQLITE_OK){
			cout << "sqlite3_exec create table err:" << sqlite3_errmsg(db) << endl;
			sqlite3_free(errmsg);

			return;
		}
	
		char *Sales_log_table = "create table if not exists Sales_log_table(MachineId char, Sales double, Date char)";
		ret = sqlite3_exec(db, Sales_log_table, NULL, NULL, &errmsg);
		if(ret != SQLITE_OK){
			cout << "sqlite3_exec create table err:" << sqlite3_errmsg(db) << endl;
			sqlite3_free(errmsg);

			return;
		}
	
		return;
	}

	//登录，将帐号密码与数据库比对
	 void Account_login(int comm_fd, cJSON *arrayData){
		 Sqlite3_open();
		 bzero(buff, sizeof(buff));

		 acc = cJSON_GetObjectItem(arrayData,"Account");
		 pas = cJSON_GetObjectItem(arrayData,"Password");

		 sprintf(buff, "select * from Account_table where account = '%s'", acc->valuestring);
		 sqlite3_get_table(db, buff, &result, &row, &column, &errmsg);
		 if(row == 0){
			 cout << "登录失败，用户名不存在" << endl;
			 Func_pack(comm_fd, USER_NAME_ERR);

			 return;
		 }
		 bzero(buff, sizeof(buff));

		 sprintf(buff, "select * from Account_table where account = '%s' and Password = '%s'", acc->valuestring, pas->valuestring);
		 sqlite3_get_table(db, buff, &result, &row, &column, &errmsg);
		 if(row == 0){
			 cout << "登录失败，密码错误" << endl;
			 Func_pack(comm_fd, PASSWORD_ERR);
		 }
		 else{
			 cout << "登录成功" << endl;
			 Func_pack(comm_fd, LOG_IN_SUCCESS);
		 }
		 bzero(buff, sizeof(buff));

		 return;
	 }	

	//注册账户，将信息写入帐号密码表
	void Account_registration(int comm_fd, cJSON *arrayData){
		Sqlite3_open();

		acc = cJSON_GetObjectItem(arrayData,"Account");
		pas = cJSON_GetObjectItem(arrayData,"Password");
		que = cJSON_GetObjectItem(arrayData,"Question");
		ans = cJSON_GetObjectItem(arrayData,"Answer");
		
		sprintf(buff, "select * from Account_table where account = '%s'", acc->valuestring);
		sqlite3_get_table(db, buff, &result, &row, &column, &errmsg);
		if(row > 0){   //用户名已存在
			cout << "注册失败，用户名已存在" << endl;
			Func_pack(comm_fd, REGISTER_FAIL);
			
			return;
		}
		bzero(buff, sizeof(buff));

		sprintf(buff, "insert into Account_table values('%s', '%s', '%s', '%s')", acc->valuestring, pas->valuestring, que->valuestring, ans->valuestring);
		ret = sqlite3_exec(db, buff, NULL, NULL, &errmsg);
		if(ret != SQLITE_OK){
			cout << "sqlite3_exec insert err:" << sqlite3_errmsg(db) << endl;
			sqlite3_free(errmsg);
			
			return;
		}
		bzero(buff, sizeof(buff));

		cout << "注册成功" << endl;
		Func_pack(comm_fd, REGISTER_SUCCESS);
		
		return;
	}

	//修改密码
	void Revise_account_password(int comm_fd, cJSON *arrayData){
		Sqlite3_open();
		
		acc = cJSON_GetObjectItem(arrayData,"Account");
		pas = cJSON_GetObjectItem(arrayData,"Password");

		sprintf(buff, "update Account_table set Password = '%s' where Account = '%s'", pas->valuestring, acc->valuestring);
		ret = sqlite3_exec(db, buff, NULL, NULL, &errmsg);
		if(ret != SQLITE_OK){
			cout << "sqlite3_exec insert err:" << sqlite3_errmsg(db) << endl;
			sqlite3_free(errmsg);
			
			return;
		}
		bzero(buff, sizeof(buff));

		cout << "修改密码成功" << endl;
		Func_pack(comm_fd, CHANGE_PASSWORD_SUCCESS);
		
		return;
	}
	
	//找回密码
	void Retrieve_password(int comm_fd, cJSON *arrayData){
		Sqlite3_open();

		cJSON *array = cJSON_CreateArray();
		acc = cJSON_GetObjectItem(arrayData,"Account");

		sprintf(buff, "select * from Account_table where Account = '%s'", acc->valuestring);
		sqlite3_get_table(db, buff, &result, &row, &column, &errmsg);
		if(row == 0){   //账户不存在
			cout << "找回密码失败，账户不存在" << endl;
			Func_pack(comm_fd, RETRIEVE_PASSWORD_ERR);
			
			return;
		}
		bzero(buff, sizeof(buff));

		for(int i = 1; i < (row + 1) * column; ++i){
			if(i % column == 0){
				cJSON *json_ret2 = cJSON_CreateObject();
				cJSON_AddItemToObject(json_ret2, "Question", cJSON_CreateString(result[i + 2]));
				cJSON_AddItemToObject(json_ret2, "Answer", cJSON_CreateString(result[i + 3]));
				cJSON_AddItemToArray(array, json_ret2);
			}
		}
		DataArray_pack(comm_fd, RETRIEVE_PASSWORD_SUCCESS, array);

		return;	
	}

	//注册柜机
	void Band_machine(int comm_fd, cJSON *arrayData){
		Sqlite3_open();

		acc = cJSON_GetObjectItem(arrayData,"Account");
		machine_id = cJSON_GetObjectItem(arrayData,"MachineId");
		machine_pas = cJSON_GetObjectItem(arrayData,"Password");

		sprintf(buff, "select * from Machine_table where MachineId = '%s'", machine_id->valuestring);
		sqlite3_get_table(db, buff, &result, &row, &column, &errmsg);
		if(row > 0){   //柜机号已存在
			cout << "注册失败，柜机号已存在" << endl;
			Func_pack(comm_fd, BIND_MACHINE_ERR);
			
			return;
		}
		bzero(buff, sizeof(buff));

		sprintf(buff, "insert into Machine_table values('%s', '%s', '%s')", machine_id->valuestring, machine_pas->valuestring, acc->valuestring);
		ret = sqlite3_exec(db, buff, NULL, NULL, &errmsg);
		if(ret != SQLITE_OK){
			cout << "sqlite3_exec insert err:" << sqlite3_errmsg(db) << endl;
			sqlite3_free(errmsg);
			
			return;
		}
		bzero(buff, sizeof(buff));

		cout << "注册柜机成功" << endl;
		Func_pack(comm_fd, BIND_MACHINE_SUCCESS);
		
		return;
	}

	//修改柜机密码
	void Revise_machine_password(int comm_fd, cJSON *arrayData){
		Sqlite3_open();
		
		machine_id   = cJSON_GetObjectItem(arrayData,"MachineId");
		machine_pas   = cJSON_GetObjectItem(arrayData,"Password");

		sprintf(buff, "update Machine_table set Password = '%s' where MachineId = '%s'", machine_pas->valuestring, machine_id->valuestring);
		ret = sqlite3_exec(db, buff, NULL, NULL, &errmsg);
		if(ret != SQLITE_OK){
			cout << "sqlite3_exec insert err:" << sqlite3_errmsg(db) << endl;
			sqlite3_free(errmsg);
			
			return;
		}
		bzero(buff, sizeof(buff));

		cout << "修改柜机密码成功" << endl;
		Func_pack(comm_fd, CHANGE_MACHINE_PASSWORD_SUCCESS);
		
		return;
	}

	//移除柜机
	void Remove_machine(int comm_fd, cJSON *arrayData){
		Sqlite3_open();

		machine_id = cJSON_GetObjectItem(arrayData,"MachineId");

		cout << machine_id->valuestring << endl;
		sprintf(buff, "delete from Machine_table where MachineId = '%s'", machine_id->valuestring);
		ret = sqlite3_exec(db, buff, NULL, NULL, &errmsg);
		if(ret != SQLITE_OK){
			cout << "sqlite3_exec insert err:" << sqlite3_errmsg(db) << endl;
			sqlite3_free(errmsg);
			
			return;
		}
		bzero(buff, sizeof(buff));

		cout << "移除柜机成功" << endl;
		Func_pack(comm_fd, REMOVE_MACHINE_SUCCESS);
		
		return;
	}

	//选择柜机
	void Choise_machine(int comm_fd, cJSON *arrayData){
		Sqlite3_open();

		cJSON *array = cJSON_CreateArray();
		acc = cJSON_GetObjectItem(arrayData,"Account");

		sprintf(buff, "select * from Machine_table where Master = '%s'", acc->valuestring);
		sqlite3_get_table(db, buff, &result, &row, &column, &errmsg);
		if(row == 0){   //没有找到柜机
			cout << "还没有注册柜机" << endl;
			Func_pack(comm_fd, CHOOSE_MAC_FAIL);
			
			return;
		}
		bzero(buff, sizeof(buff));

		for(int i = 1; i < (row + 1) * column; ++i){
			if(i % column == 0){
				cJSON *json_ret2 = cJSON_CreateObject();
				cJSON_AddItemToObject(json_ret2, "MachineId", cJSON_CreateString(result[i]));
				cJSON_AddItemToArray(array, json_ret2);
			}
		}
		 DataArray_pack(comm_fd, CHOOSE_MAC_SUCCESS, array);

		return;
	}

	//增加商品信息
	void Add_commodity(int comm_fd, cJSON *arrayData){
		Sqlite3_open();
		
		cJSON *array = cJSON_CreateArray();
		machine_id   = cJSON_GetObjectItem(arrayData,"MachineId");
		GoodsId      = cJSON_GetObjectItem(arrayData,"GoodsId");
		Name         = cJSON_GetObjectItem(arrayData,"Name");
		Price        = cJSON_GetObjectItem(arrayData,"Price");
		Stock        = cJSON_GetObjectItem(arrayData,"Stock");
		Image        = cJSON_GetObjectItem(arrayData,"Image");
		Type         = cJSON_GetObjectItem(arrayData,"Type");

		sprintf(buff, "select * from Commodity_table where GoodsId = '%s' and MachineId = '%s'", GoodsId->valuestring, machine_id->valuestring);
		sqlite3_get_table(db, buff, &result, &row, &column, &errmsg);
		if(row > 0){   //商品已存在
			cout << "注册失败，商品已存在" << endl;
			Func_pack(comm_fd, ADD_COMMODITY_DATA_FAIL);
			
			return;
		}
		bzero(buff, sizeof(buff));

		sprintf(buff, "insert into Commodity_table values('%s', '%s', '%s', '%f', '%d', '%s', '%s')", machine_id->valuestring, GoodsId->valuestring, Name->valuestring, Price->valuedouble, Stock->valueint, Image->valuestring, Type->valuestring);
		ret = sqlite3_exec(db, buff, NULL, NULL, &errmsg);
		if(ret != SQLITE_OK){
			cout << "sqlite3_exec insert err:" << sqlite3_errmsg(db) << endl;
			sqlite3_free(errmsg);
			
			return;
		}
		bzero(buff, sizeof(buff));

		cout << "添加商品成功" << endl;
		Func_pack(comm_fd, ADD_COMMODITY_DATA_SUCCESS);
		
		return;
	}
	
	//删除商品信息
	void Remove_commodity(int comm_fd, cJSON *arrayData){
		Sqlite3_open();

		machine_id = cJSON_GetObjectItem(arrayData,"MachineId");
		GoodsId = cJSON_GetObjectItem(arrayData,"GoodsId");

		sprintf(buff, "delete from Commodity_table where MachineId = '%s' and GoodsId = '%s'", machine_id->valuestring, GoodsId->valuestring);
		ret = sqlite3_exec(db, buff, NULL, NULL, &errmsg);
		if(ret != SQLITE_OK){
			cout << "sqlite3_exec insert err:" << sqlite3_errmsg(db) << endl;
			sqlite3_free(errmsg);
			
			return;
		}
		bzero(buff, sizeof(buff));

		cout << "下架商品成功" << endl;
		Func_pack(comm_fd, REMOVE_COMMODITY_DATA_SUCCESS);
		
		return;
	}
	
	//修改商品信息
	void Change_commodity(int comm_fd, cJSON *arrayData){	
		Sqlite3_open();
		
		cJSON *array = cJSON_CreateArray();
		machine_id   = cJSON_GetObjectItem(arrayData,"MachineId");
		GoodsId      = cJSON_GetObjectItem(arrayData,"GoodsId");
		Name         = cJSON_GetObjectItem(arrayData,"Name");
		Price        = cJSON_GetObjectItem(arrayData,"Price");
		Stock        = cJSON_GetObjectItem(arrayData,"Stock");
		Image        = cJSON_GetObjectItem(arrayData,"Image");
		Type         = cJSON_GetObjectItem(arrayData,"Type");

		sprintf(buff, "update Commodity_table set Name = '%s', Price = '%f', Stock = '%d', Image = '%s', Type = '%s' where MachineId = '%s' and GoodsId = '%s'", Name->valuestring, Price->valuedouble, Stock->valueint, Image->valuestring, Type->valuestring, machine_id->valuestring, GoodsId->valuestring);
		ret = sqlite3_exec(db, buff, NULL, NULL, &errmsg);
		if(ret != SQLITE_OK){
			cout << "sqlite3_exec insert err:" << sqlite3_errmsg(db) << endl;
			sqlite3_free(errmsg);
			
			return;
		}
		bzero(buff, sizeof(buff));

		cout << "修改商品信息成功" << endl;
		Func_pack(comm_fd, CHANGE_COMMODITY_DATA_SUCCESS);
		
		return;
	}
	
	//查看商品信息
	void Check_commodity(int comm_fd, cJSON *arrayData){
		Sqlite3_open();
		
		cJSON *array = cJSON_CreateArray();
		machine_id = cJSON_GetObjectItem(arrayData,"MachineId");

		sprintf(buff, "select * from Commodity_table where MachineId = '%s'", machine_id->valuestring);
		sqlite3_get_table(db, buff, &result, &row, &column, &errmsg);
		if(row == 0){   //没有找到商品
			cout << "还没有添加商品" << endl;
			Func_pack(comm_fd, CHECK_COMMODITY_DATA_FAIL);
			
			return;
		}
		bzero(buff, sizeof(buff));

		for(int i = 1; i < (row + 1) * column; ++i){
			if(i % column == 0){
				cJSON *json_ret2 = cJSON_CreateObject();
				cJSON_AddItemToObject(json_ret2, "GoodsId", cJSON_CreateString(result[i + 1]));
				cJSON_AddItemToObject(json_ret2, "Name", cJSON_CreateString(result[i + 2]));
				cJSON_AddItemToObject(json_ret2, "Price", cJSON_CreateNumber(*result[i + 3]));
				cJSON_AddItemToObject(json_ret2, "Stock", cJSON_CreateNumber(*result[i + 4]));
				cJSON_AddItemToObject(json_ret2, "Image", cJSON_CreateString(result[i + 5]));
				cJSON_AddItemToObject(json_ret2, "Type", cJSON_CreateString(result[i + 6]));
				cJSON_AddItemToArray(array, json_ret2);
			}
		}
		DataArray_pack(comm_fd, CHECK_COMMODITY_DATA_SUCCESS, array);

		return;
	}

	//查看柜机销售额
	void Check_commodity_sale(int comm_fd, cJSON *arrayData){
		Sqlite3_open();

		cJSON *array = cJSON_CreateArray();
		machine_id = cJSON_GetObjectItem(arrayData, "MachineId");

		sprintf(buff, "select * from Sales_log_table where MachineId = '%s'", machine_id->valuestring);
		sqlite3_get_table(db, buff, &result, &row, &column, &errmsg);
		if(row == 0){   //没有找到柜机
			cout << "柜机不存在" << endl;
			Func_pack(comm_fd, CHECK_COMMODITY_SALE_FAIL);
			
			return;
		}
		bzero(buff, sizeof(buff));

		for(int i = 1; i < (row + 1) * column; ++i){
			if(i % column == 0){
				cJSON *json_ret2 = cJSON_CreateObject();
				cJSON_AddItemToObject(json_ret2, "Sales", cJSON_CreateString(result[i + 1]));
				cJSON_AddItemToObject(json_ret2, "Date", cJSON_CreateString(result[i + 2]));
				cJSON_AddItemToArray(array, json_ret2);
			}
		}
		DataArray_pack(comm_fd, CHECK_COMMODITY_SALE_SUCCESS, array);
		return;
	}

	//仅有功能号的json包封装
	void Func_pack(int comm_fd, int func){
		cJSON_AddItemToObject(json_ret, "Turban", cJSON_CreateString("0xbb"));
		cJSON_AddItemToObject(json_ret, "Function", cJSON_CreateNumber(func));
		json_ptr = cJSON_Print(json_ret);
		cout << json_ptr << endl;
		write(comm_fd, json_ptr, strlen(json_ptr));	
	
		return;
	}

	//带有DataArray的json包封装
	void DataArray_pack(int comm_fd, int func, cJSON *array){
		cJSON_AddItemToObject(json_ret, "Turban", cJSON_CreateString("0xbb"));
		cJSON_AddItemToObject(json_ret, "Function", cJSON_CreateNumber(func));
		cJSON_AddItemToObject(json_ret, "DataArray", array);
		json_ptr = cJSON_Print(json_ret);
		cout << json_ptr << endl;
		write(comm_fd, json_ptr, strlen(json_ptr));	
		
		return;
	}
	
private:
	sqlite3 *db = NULL;
	char buff[MAXLINE];
	char **result = NULL;
	int row = -1;
	int column = -1;
	int ret;
	char *errmsg;
	char *json_ptr = NULL;
	cJSON *json_ret = cJSON_CreateObject();  //第一层键值
	cJSON *acc;           //帐号
	cJSON *pas;           //密码
	cJSON *que;           //密保问题
	cJSON *ans;           //密保答案
	cJSON *machine_id;    //柜机号
	cJSON *machine_pas;   //柜机密码
	cJSON *GoodsId;       //商品ID
	cJSON *Name;          //商品名
	cJSON *Price;         //商品价格
	cJSON *Stock;         //商品库存
	cJSON *Image;         //商品图片
	cJSON *Type;          //商品类型
};


#endif
